package common

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
	"git.myscrm.cn/golang/stark/v4"
)

type CreateTenantRepository struct {
}

func NewCreateTenantRepository() CreateTenantReposIface {
	return &CreateTenantRepository{}
}

// CreateTenant 创建租户
func (r *CreateTenantRepository) CreateTenant(ctx context.Context, request *pb.CreateTenantRequest) (*pb.CreateTenantResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "CreateTenant request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCreateTenantServiceClient(conn)
	res, err := client.CreateTenant(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CreateTenant request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// RemoveTenant 租户销户
func (r *CreateTenantRepository) RemoveTenant(ctx context.Context, request *pb.RemoveTenantRequest) (*pb.RemoveTenantResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "RemoveTenant request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCreateTenantServiceClient(conn)
	res, err := client.RemoveTenant(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "RemoveTenant request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetTaskStatus 获取任务状态
func (r *CreateTenantRepository) GetTaskStatus(ctx context.Context, request *pb.GetTaskStatusRequest) (*pb.GetTaskStatusResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTaskStatus request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCreateTenantServiceClient(conn)
	res, err := client.GetTaskStatus(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTaskStatus request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// CreateTenantCallback 开库结果回调
func (r *CreateTenantRepository) CreateTenantCallback(ctx context.Context, request *pb.CreateTenantCallbackRequest) (*pb.CreateTenantCallbackResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "CreateTenantCallback request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCreateTenantServiceClient(conn)
	res, err := client.CreateTenantCallback(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CreateTenantCallback request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
